package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Duty;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.DutyMapper;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.DutyQueryObject;
import cn.wolfcode.crm.service.IDutyService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class DutyServiceImpl implements IDutyService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DutyMapper dutyMapper;

    @Override
    public Duty selectByPrimaryKey(Long id) {
        return dutyMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Duty> selectAll() {
        return dutyMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(Duty entity) {
        Subject subject = SecurityUtils.getSubject();
        Employee principal = (Employee)subject.getPrincipal();

        Calendar calendar = Calendar.getInstance();
        Date time = calendar.getTime();

        List<Duty> duties = dutyMapper.checkreSignOn(time, principal.getId());


        if(duties ==null){
            principal = employeeMapper.selectByPrimaryKey(principal.getId());
            entity.setEmployee(principal);
            entity.setDepartment(principal.getDept());
            entity.setSignOn(time);
            entity.setDate(time);
            int i1 = calendar.get(Calendar.HOUR_OF_DAY);
            if(i1>=10){
                entity.setState(false);
            }
            dutyMapper.insert(entity);
        }else{
            throw new RuntimeException("今天已经打卡了");
        }
    }


    @Override
    public PageResult query(DutyQueryObject qo) {
        int total = dutyMapper.query4Count(qo);
        if(total == 0 ){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<?> rows = dutyMapper.query4List(qo);
        return new PageResult(total,rows);
    }


    public void signOff() {
        Subject subject = SecurityUtils.getSubject();
        Employee principal = (Employee)subject.getPrincipal();

        Calendar calendar = Calendar.getInstance();
        Date time = calendar.getTime();

            if(calendar.get(Calendar.HOUR_OF_DAY)<=17){
                throw new RuntimeException("未到下班时间!(下午5点)");
            }

            List<Duty> duties =dutyMapper.checkreSignOn(time, principal.getId());
            /**
             * 没有签到,直接签退的情况,直接新插入sql,记录的是signOff列
             */
            if (duties == null) {
                Duty entity = new Duty();
                principal = employeeMapper.selectByPrimaryKey(principal.getId());
                entity.setEmployee(principal);
                entity.setDepartment(principal.getDept());
                entity.setSignOff(time);
                entity.setDate(time);
                dutyMapper.insert(entity);
            }
            else{
                //已经签到了,就直接定位到当天签到的数据,更新signOff列
                dutyMapper.signOff(new Date(),principal.getId());
            }





    }

}
